package view.orderfood;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Component;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import entiey.Dictionary;
import entiey.Menu;
import entiey.OrderFood;
import entiey.Table;
import entiey.TypeId;
import entiey.Waiter;
import service.DictionaryService;
import service.MenuService;
import service.OrderFoodService;
import service.TableService;
import service.WaiterService;
import service.impl.DictionaryServiceImpl;
import service.impl.MenuServiceImpl;
import service.impl.OrderFoodServiceImpl;
import service.impl.TableServiceImpl;
import service.impl.WaiterServiceImpl;
import util.AutoNo;
import util.CastUtil;
import view.main.BtnActionListener;
import view.settlement.V_Pay;
import view.table.V_tables;

import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JPopupMenu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.JComboBox;
/**
 * 点餐页面
 * @author 赵红娇
 * view.orderfood
 * @date 2020年8月30日下午5:39:32
 */
public class V_order extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private MenuService ms=new MenuServiceImpl();
	private JPanel panel_3;
	private JPanel panel_4;
	private JPanel panel_5;
	private JLabel label2;
	private List<JButton> btnList=new ArrayList<>();//菜单显示的btn列表
	private List<JLabel> labels=new ArrayList<JLabel>();//记录菜单显示的菜名列表
	private List<OrderFood> menuList=new ArrayList<>();//存储客户点菜的列表(只是先点菜，还未生成订单)
	private JTextField text_orderNum;
	private String remark ;
	private JLabel lbl_totalprice;
	private JLabel lbl_tname;
	private JTabbedPane tabbedPane;
	private int selectedIndex=0;//获取选中标签页得下标
	private JPanel panel_6;
	private JPanel panel_7;
	private JPanel panel_9;
	private JPanel panel_10;
	private DictionaryService ds=new DictionaryServiceImpl();
	private int waiterId;//选择的服务员id
	private int tableId;//选择桌子的id

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_order frame = new V_order();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	private int type;//菜品类型
	private List<Menu> menu;//根据类型查找到的菜单列表
	private JButton btnNewButton;
	private JLabel lbl_orderNo;
	private JComboBox cb_waiter;
	public V_order() {
		setTitle("菜单");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 847, 640);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				selectedIndex= tabbedPane.getSelectedIndex();
				switch (selectedIndex) {
				//默认显示全部菜品页面
				case 1:
					panel_4.removeAll();
					//查找全部凉菜(先查找凉菜在字典表中的id,然后根据id在菜单表里查找凉菜)
					//根据名称和菜品的typeId共同查找，得到凉菜在字典表中的id
					Dictionary cold= ds.getDictionaryByTypeIDAndName("凉菜", TypeId.菜单类别);
					type= cold.getId();
					menu= ms.findByType(type);
					showMenu(menu,panel_4);
					//按钮的监听事件
					btn_listener();
					break;
				case 2:
					panel_5.removeAll();
					//查找全部热菜(先查找凉菜在字典表中的id,然后根据id在菜单表里查找凉菜)
					//根据名称和菜品的typeId共同查找，得到凉菜在字典表中的id
					Dictionary hot= ds.getDictionaryByTypeIDAndName("热菜", TypeId.菜单类别);
					type= hot.getId();
					menu= ms.findByType(type);
					showMenu(menu,panel_5);
					//按钮的监听事件
					btn_listener();
					break;
				case 3:
					panel_6.removeAll();
					//查找全部甜品(先查找凉菜在字典表中的id,然后根据id在菜单表里查找甜品)
					//根据名称和菜品的typeId共同查找，得到甜品在字典表中的id
					Dictionary sweet= ds.getDictionaryByTypeIDAndName("甜品", TypeId.菜单类别);
					type= sweet.getId();
					menu= ms.findByType(type);
					showMenu(menu,panel_6);
					//按钮的监听事件
					btn_listener();
					break;
				case 4:
					panel_7.removeAll();
					//查找全部汤(先查找凉菜在字典表中的id,然后根据id在菜单表里查找汤)
					//根据名称和菜品的typeId共同查找，得到汤在字典表中的id
					Dictionary soup= ds.getDictionaryByTypeIDAndName("汤", TypeId.菜单类别);
					type= soup.getId();
					menu= ms.findByType(type);
					showMenu(menu,panel_7);
					//按钮的监听事件
					btn_listener();
					break;
				case 5:
					panel_9.removeAll();
					//查找全部套餐(先查找凉菜在字典表中的id,然后根据id在菜单表里查找套餐)
					//根据名称和菜品的typeId共同查找，得到套餐在字典表中的id
					MenuService menuService=new MenuServiceImpl();
//					menuService.findByComId();
					showMenu(menu,panel_9);
					//按钮的监听事件
					btn_listener();
					break;
				}
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
						.addComponent(tabbedPane, Alignment.LEADING)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE))
					.addGap(4))
		);
		
		//根据menu表长度,显示全部菜单
		panel_3 = new JPanel();
		List<Menu> list = ms.findAll();
		showMenu(list,panel_3);
		tabbedPane.addTab("全部", null, panel_3, null);
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3.setHorizontalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGap(0, 499, Short.MAX_VALUE)
		);
		gl_panel_3.setVerticalGroup(
			gl_panel_3.createParallelGroup(Alignment.LEADING)
				.addGap(0, 495, Short.MAX_VALUE)
		);
		panel_3.setLayout(gl_panel_3);
		
//--------------------------------------------------------------------------------
		//显示全部凉菜
		panel_4 = new JPanel();
		tabbedPane.addTab("凉菜", null, panel_4, null);
		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4.setHorizontalGroup(
				gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGap(0, 499, Short.MAX_VALUE)
		);
		gl_panel_4.setVerticalGroup(
				gl_panel_4.createParallelGroup(Alignment.LEADING)
				.addGap(0, 495, Short.MAX_VALUE)
		);
		panel_4.setLayout(gl_panel_4);
		
		panel_5 = new JPanel();
		tabbedPane.addTab("热菜", null, panel_5, null);
		GroupLayout gl_panel_5 = new GroupLayout(panel_5);
		gl_panel_5.setHorizontalGroup(
				gl_panel_5.createParallelGroup(Alignment.LEADING)
				.addGap(0, 499, Short.MAX_VALUE)
		);
		gl_panel_5.setVerticalGroup(
				gl_panel_5.createParallelGroup(Alignment.LEADING)
				.addGap(0, 495, Short.MAX_VALUE)
		);
		panel_5.setLayout(gl_panel_5);
		
		panel_6 = new JPanel();
		tabbedPane.addTab("甜品", null, panel_6, null);
		GroupLayout gl_panel_6 = new GroupLayout(panel_6);
		gl_panel_6.setHorizontalGroup(
				gl_panel_6.createParallelGroup(Alignment.LEADING)
				.addGap(0, 499, Short.MAX_VALUE)
		);
		gl_panel_6.setVerticalGroup(
				gl_panel_6.createParallelGroup(Alignment.LEADING)
				.addGap(0, 495, Short.MAX_VALUE)
		);
		panel_6.setLayout(gl_panel_6);
		
		panel_7 = new JPanel();
		tabbedPane.addTab("汤", null, panel_7, null);
		GroupLayout gl_panel_7 = new GroupLayout(panel_7);
		gl_panel_7.setHorizontalGroup(
				gl_panel_7.createParallelGroup(Alignment.LEADING)
				.addGap(0, 499, Short.MAX_VALUE)
		);
		gl_panel_7.setVerticalGroup(
				gl_panel_7.createParallelGroup(Alignment.LEADING)
				.addGap(0, 495, Short.MAX_VALUE)
		);
		panel_7.setLayout(gl_panel_7);
		
		panel_9 = new JPanel();
		tabbedPane.addTab("套餐", null, panel_9, null);
		GroupLayout gl_panel_9 = new GroupLayout(panel_9);
		gl_panel_9.setHorizontalGroup(
				gl_panel_9.createParallelGroup(Alignment.LEADING)
				.addGap(0, 499, Short.MAX_VALUE)
		);
		gl_panel_9.setVerticalGroup(
				gl_panel_9.createParallelGroup(Alignment.LEADING)
				.addGap(0, 495, Short.MAX_VALUE)
		);
		panel_9.setLayout(gl_panel_9);
		
		
		JLabel lblNewLabel = new JLabel("餐桌:");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		
		JPanel panel_1 = new JPanel();
		
		lbl_tname = new JLabel("未选择餐桌");
		lbl_tname.setForeground(Color.RED);
		
		JLabel lblNewLabel_1 = new JLabel("服务员：");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 15));
		
		btnNewButton = new JButton("选择餐桌");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				V_tables tables=new V_tables();
				tables.setBounds(V_order.this.getX(),V_order.this.getY(),tables.getWidth(),tables.getHeight());
				tables.setVisible(true);
				tables.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						tableId = tables.getTableId();
						TableService tableService=new TableServiceImpl();
						Table getTable = tableService.findById(tableId);
						lbl_tname.setText(getTable.getNode());
					}
				});
			}
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 25));
		
		JLabel lblNewLabel_2 = new JLabel("单号：");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 20));
		
		lbl_orderNo = new JLabel("未选择餐桌");
		lbl_orderNo.setForeground(Color.RED);
		
		cb_waiter = new JComboBox();
		cb_waiter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Waiter waiter = (Waiter)cb_waiter.getSelectedItem();
				waiterId=waiter.getId();
				System.out.println(waiterId);
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGap(18)
					.addComponent(lbl_tname, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGap(18)
					.addComponent(lblNewLabel_1)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(cb_waiter, GroupLayout.PREFERRED_SIZE, 113, GroupLayout.PREFERRED_SIZE)
					.addGap(19))
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
					.addGap(3)
					.addComponent(lbl_orderNo, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(btnNewButton, GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)
					.addGap(63))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(13)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
						.addComponent(lbl_tname)
						.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
						.addComponent(cb_waiter, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(9)
							.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(20)
							.addComponent(lbl_orderNo)))
					.addPreferredGap(ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 467, GroupLayout.PREFERRED_SIZE))
		);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		panel_1.add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"menuList\u4E0B\u6807", "\u83DC\u540D", "\u5355\u4EF7", "\u6570\u91CF", "\u5907\u6CE8"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(0);
		table.getColumnModel().getColumn(0).setMinWidth(0);
		table.getColumnModel().getColumn(0).setMaxWidth(0);
		table.getColumnModel().getColumn(4).setPreferredWidth(86);
		table.getColumnModel().getColumn(4).setMinWidth(40);
		scrollPane.setViewportView(table);
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("数量+1");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取选中行
				int selectedRow = table.getSelectedRow();
				//获取选中行的第0列（第0列保存的是menuList的下标）
				int index = (int) table.getValueAt(selectedRow, 0);
				//根据下标取出订单，更新数量
				OrderFood orderFood = menuList.get(index);
				orderFood.setFoodNum(orderFood.getFoodNum()+1);
				//在把更新过之后的订单，覆盖menuList中原来的订单
				initTable(menuList);
			}
		});
		popupMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("数量-1");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取选中行
				int selectedRow = table.getSelectedRow();
				//获取选中行的第0列（第0列保存的是menuList的下标）
				int index = (int) table.getValueAt(selectedRow, 0);
				//根据下标取出订单，更新数量
				OrderFood orderFood = menuList.get(index);
				orderFood.setFoodNum(orderFood.getFoodNum()-1);
				System.out.println("菜单数量"+orderFood.getFoodNum());
				if(orderFood.getFoodNum()==0) {
					menuList.remove(index);
				}
				//在把更新过之后的订单，覆盖menuList中原来的订单
				initTable(menuList);
			}
		});
		popupMenu.add(mntmNewMenuItem_1);
		
		JMenuItem mntmNewMenuItem_2 = new JMenuItem("删除菜品");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//获取选中行
				int selectedRow = table.getSelectedRow();
				//获取选中行的第0列（第0列保存的是menuList的下标）
				int index = (int) table.getValueAt(selectedRow, 0);
				menuList.remove(index);
				initTable(menuList);
			}
		});
		popupMenu.add(mntmNewMenuItem_2);
		
		JMenuItem mntmNewMenuItem_3 = new JMenuItem("添加备注");
		mntmNewMenuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				V_desc desc=new V_desc();
				desc.setBounds(V_order.this.getX(),V_order.this.getY(),desc.getWidth(),desc.getHeight());
				desc.setVisible(true);
				desc.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosed(WindowEvent e) {
						remark= desc.getRemark();
						//获取选中行
						int selectedRow = table.getSelectedRow();
						//获取选中行的第0列（第0列保存的是menuList的下标）
						int index = (int) table.getValueAt(selectedRow, 0);
						OrderFood orderFood = menuList.get(index);
						orderFood.setRemark(remark);
						initTable(menuList);
					}
				});
			}
		});
		popupMenu.add(mntmNewMenuItem_3);
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.SOUTH);
		
		text_orderNum = new JTextField();
		text_orderNum.setFont(new Font("宋体", Font.PLAIN, 16));
		text_orderNum.setForeground(Color.RED);
		text_orderNum.setEditable(false);
		text_orderNum.setText("已点菜品：0");
		text_orderNum.setColumns(10);
		
		lbl_totalprice = new JLabel("总金额:￥0");
		lbl_totalprice.setFont(new Font("宋体", Font.PLAIN, 16));
		lbl_totalprice.setForeground(Color.RED);
		
		JButton btnNewButton_2 = new JButton("下单");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean flag = false;
				for (OrderFood food : menuList) {
					//当点击下单的时候，才生成订单号
					food.setOrderNo(lbl_orderNo.getText());
					food.setWid(waiterId);
					OrderFoodService foodService = new OrderFoodServiceImpl();
					flag = foodService.add(food);
				}
				if (flag) {
					JOptionPane.showMessageDialog(V_order.this, "下单成功", "成功提示", JOptionPane.INFORMATION_MESSAGE);
					// 下单成功之后，清空当前页面上的所有信息
					clear();
					//初始化结算页面
//					V_Pay pay=new V_Pay();
					BtnActionListener.getPay().initComBox();
					BtnActionListener.getPay().initTable(new OrderFoodServiceImpl().findAll());
				} else {
					JOptionPane.showMessageDialog(V_order.this, "下单失败", "失败提示", JOptionPane.ERROR_MESSAGE);
				}
			}

		});
		btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 16));
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addComponent(text_orderNum, GroupLayout.PREFERRED_SIZE, 103, GroupLayout.PREFERRED_SIZE)
					.addGap(18, 18, Short.MAX_VALUE)
					.addComponent(lbl_totalprice, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnNewButton_2)
					.addGap(4))
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(lbl_totalprice)
						.addComponent(text_orderNum, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton_2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addGap(12))
		);
		panel_2.setLayout(gl_panel_2);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
		
		//按钮的监听事件
		btn_listener();
		initWaiter();//初始化服务员下拉框
		
		lbl_orderNo.setText(AutoNo.autoOrderFoodNo());
		
		//不选择服务员默认是第一个
		Waiter waiter = (Waiter)cb_waiter.getSelectedItem();
		waiterId=waiter.getId();
	}
	//显示菜单
	public void showMenu(List<Menu> list,JPanel panel) {
		int x=10;
		int y=10;
		int width=130;
		int height=100;
		for (int i = 0; i < list.size(); i++) {
			Menu menu=list.get(i);
			//读出菜品图片,设置成Icon图标来进行显示
			JPanel panel_8 = new JPanel();
			panel_8.setBounds(x, y, width, height);
			panel_8.setBorder(new LineBorder(Color.black));
			panel_8.setVisible(true);
			panel_8.setLayout(new BorderLayout(0, 0));
			JLabel menuImg = new JLabel("菜品图片");
			//创建一个icon图片
			ImageIcon icon=new ImageIcon(list.get(i).getFoodIcon());
			icon.setImage(icon.getImage().getScaledInstance(130, 100, 1));
			menuImg.setIcon(icon);
			menuImg.setBounds(x, y, width, height);
			panel_8.add(menuImg, BorderLayout.CENTER);
			//本标签用来显示菜名
			JLabel label2=new JLabel();
			label2.setText(menu.getFoodName()+"   "+menu.getFoodPrice());
			label2.setForeground(Color.red);
			labels.add(label2);
			panel_8.add(label2, BorderLayout.PAGE_START);
			//选择按钮
			JButton button=new JButton("选择");
			btnList.add(button);
			panel_8.add(button,BorderLayout.PAGE_END);
			//根据不同的分页标签了下标，来确定到底在哪个页面显示
			panel.add(panel_8);
			x+=140;
			if(i==5) {
				x=0;
				y+=110;
			}
		}
	}
	/**
	 * 选择菜品按钮的监听事件
	 */
	public void btn_listener() {
		System.out.println(btnList.size());
		for (int i=0;i<btnList.size();i++) {
			JButton jButton=btnList.get(i);
			JLabel jLabel=labels.get(i);
			jButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					//获取菜品名称和价格
					String menuinfo = jLabel.getText();
					String menuName = menuinfo.substring(0,menuinfo.indexOf(" "));
					double menuPrice =CastUtil.castDouble(menuinfo.substring(menuinfo.lastIndexOf(" ")));
					//根据菜名和价格进行查找 
					Menu menu = ms.findByNameAndPrice(menuName, menuPrice);
					boolean flag=false;
					for (OrderFood orderFood:menuList) {
						//如果点菜列表已经存在该菜品，数量+1
						if(menu.getId()==orderFood.getMid()) {
							flag=true;
							orderFood.setFoodNum(orderFood.getFoodNum()+1);
						}
					}
					
					//点菜列表里不存在该菜品，则添加菜品
					if(flag==false) {
						OrderFood food=new OrderFood();
						System.out.println(waiterId);
						food.setWid(waiterId);//服务员id
						food.setTid(tableId);//桌子id
						food.setMid(menu.getId());//菜单编号
						food.setFoodName(menu.getFoodName());//菜品名字
						food.setFoodPrice(menu.getFoodPrice());//菜品价格
						food.setFoodNum(1);
						menuList.add(food);
					}
					initTable(menuList);
				}
			});
		}
		btnList.removeAll(btnList);
		labels.removeAll(labels);
	}
	/**
	 * 初始化表格
	 */
	public void initTable(List<OrderFood> list) {
		//清空表格
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		int rowCount = model.getRowCount();
		if(rowCount!=0) {
			for (int i = 0; i < rowCount; i++) {
				model.removeRow(0);
			}
		}
		//初始化表格
		for (int i = 0; i < menuList.size(); i++) {
			OrderFood orderFood=menuList.get(i);
			model.addRow(new Object[] {
					i,
					orderFood.getFoodName(),
					orderFood.getFoodPrice(),
					orderFood.getFoodNum(),
					orderFood.getRemark()
				});
		}
		//计算已选择菜品数量
		int sum=0;
		for (OrderFood orderFood : list) {
			sum+=orderFood.getFoodNum();
		}
		text_orderNum.setText("已选菜品："+sum);
		initlbl();//计算菜品的总价钱
	}
	//总金额随选中的菜品单价一直在发生变化
	public void initlbl() {
		int totalPrice=0;
		for (OrderFood food:menuList) {
			totalPrice+=food.getFoodPrice()*food.getFoodNum();
		}
		lbl_totalprice.setText("总金额:￥"+totalPrice);
	}
	//清空界面内容
	private void clear() {
		menuList.clear();
		initTable(menuList);
		lbl_totalprice.setText("总金额:￥0");
		text_orderNum.setText("已选菜品：0");
		lbl_tname.setText("未选择餐桌");
		cb_waiter.setSelectedIndex(0);
	}
	//初始化收银员下拉框
	public void initWaiter() {
		DefaultComboBoxModel<Waiter> model=new DefaultComboBoxModel<>();
		WaiterService service=new WaiterServiceImpl();
		List<Waiter> waiterList = service.findAllWaiter();
		for (Waiter waiter : waiterList) {
			model.addElement(waiter);
		}
		cb_waiter.setModel(model);
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
